﻿using System;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using ConfigurationManagement.Data;

namespace ConfigurationManagement.Administration.UI
{
    public partial class ManageSolutionsForm : Form
    {
        private readonly ConfigurationManagementDataContext _db = new ConfigurationManagementDataContext();
        private Project _selectedProject;

        public ManageSolutionsForm()
        {
            InitializeComponent();
        }

        private void ProjectsComboBoxSelectedIndexChanged(object sender, EventArgs e)
        {
            if (ProjectsComboBox.SelectedIndex < 0) return;
            Save();
            _selectedProject = _db.Projects.Single(p => p.ProjectName == ProjectsComboBox.Text);
            LoadSolutions();
        }

        private void LoadSolutions()
        {
            if (_selectedProject == null) return;            
            SolutionsGridView.DataSource = _selectedProject.Solutions;
// ReSharper disable AssignNullToNotNullAttribute
            SolutionsGridView.Sort(SolutionsGridView.Columns["SolutionName"], ListSortDirection.Ascending);
// ReSharper restore AssignNullToNotNullAttribute
        }

        private void Save()
        {
            _db.SubmitChanges();
        }


        private void ManageSolutionsFormLoad(object sender, EventArgs e)
        {
            if (SolutionsGridView != null)
            {
                SolutionsGridView.AutoGenerateColumns = false;
                
                if (SolutionsGridView.Columns.Contains("SolutionName"))
// ReSharper disable PossibleNullReferenceException
                    SolutionsGridView.Columns["SolutionName"].DataPropertyName = "SolutionName";
// ReSharper restore PossibleNullReferenceException

                if (SolutionsGridView.Columns.Contains("SolutionDescription"))
// ReSharper disable PossibleNullReferenceException
                    SolutionsGridView.Columns["SolutionDescription"].DataPropertyName = "SolutionDescription";
// ReSharper restore PossibleNullReferenceException
            }

            ProjectsComboBox.ValueMember = "ProjectID";
            ProjectsComboBox.DisplayMember = "ProjectName";
            ProjectsComboBox.DataSource = _db.Projects.OrderBy(x => x.ProjectName);
            
            _selectedProject = _db.Projects.Single(p => p.ProjectName == ProjectsComboBox.Text);
            LoadSolutions();
        }

        private void SolutionsGridViewUserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            var solution = (Solution) e.Row.DataBoundItem;
            if (solution.SolutionSettings.Count <= 0) return;
            var dr =
                MessageBox.Show(@"This solution contains information, are you sure you wish to delete it?",
                                @"Delete Solution", MessageBoxButtons.YesNo);
            if (dr != DialogResult.Yes)
            {
                e.Cancel = true;
                return;
            }
            foreach (var setting in solution.SolutionSettings)
            {
                _db.SolutionSettings.DeleteOnSubmit(setting);
                foreach (var settingValue in setting.SettingValues)
                {
                    _db.SettingValues.DeleteOnSubmit(settingValue);
                }
            }
            _db.Solutions.DeleteOnSubmit(solution);
        }

        private void ManageSolutionsFormFormClosing(object sender, FormClosingEventArgs e)
        {
            Save();
        }
    }
}